
import java.util.*;
//import java.util.Stack;

public class Move {
    private HashMap<String, Handler> Handlers = new HashMap<>();
    private String name;
    private int grade;
    private boolean isSave=false;

    Frame frame = new Frame();
    Cube cube;
    ArrayList<int[][]> step = new ArrayList<>();
    ArrayList<Integer> gradeStep = new ArrayList<>();
//    Stack stack=new Stack


    public Move() {
        cube = new Cube();
        Handlers.put("w", new HandlerUp(this));
        Handlers.put("s", new HandlerDown(this));
        Handlers.put("a", new HandlerLeft(this));
        Handlers.put("d", new HandlerRight(this));
        Handlers.put("help", new HandlerHelp(this));
        Handlers.put("back", new HandlerBack(this));
        Handlers.put("save", new HandlerSave(this));
        Handlers.put("load", new HandlerLoad(this));
    }


    public void start() {
        Scanner in = new Scanner(System.in);
        System.out.println("我知道很简陋但你住嘴的2048开始！");
        System.out.println("如果有疑问请输入help");
        frame.printFrame(cube);
        step.add(frame.getCopyCube(cube));
        gradeStep.add(0);
        while (true) {
            String direction = in.nextLine();
            Handler handler = Handlers.get(direction);
            if (direction.equals("save")) {
                    handler.saveGame(cube);
                    isSave=true;
                break;
            }
            if (direction.equals("load")) {
                cube = handler.loadGame();
                step.removeAll(step);
                gradeStep.removeAll(gradeStep);
                gradeStep.add(handler.getGrade(cube));
                step.add(frame.getCopyCube(cube));
                frame.printFrame(cube);
            }
            if (handler != null&&!direction.equals("load")) {
                handler.doCmd(direction, cube);
                if (direction.equals("back")) {
                    frame.printFrame(cube);
                    continue;
                }
            } else {
                continue;
            }
            System.out.println("总分：" + handler.getGrade(cube));
            frame.printFrame(cube);
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (handler.getIsMove(cube)) {
                handler.reborn(cube);
                step.add(frame.getCopyCube(cube));
                gradeStep.add(handler.getGrade(cube));
                frame.setCube(cube);
            }
            handler.check(cube);
            if (handler.getIsOver(cube)) {
                System.out.println("最终得分：" + handler.getGrade(cube));
                frame.printFrame(cube);
                System.out.println("game over");
                System.out.println("请该沙雕网友留下你的大名：");
                name=in.nextLine();
                grade=handler.getGrade(cube);
                break;
            } else {
                System.out.println("请继续你的沙雕：");
                frame.printFrame(cube);
            }
        }
    }

    public int getGrade() {
        return grade;
    }

    public String getName() {
        return name;
    }

    public boolean isSave(){
        return isSave;
    }
}
